package com.hadoop.mapreduce.phoneProvince;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 将手机号按照归属地分类
 */
public class PhoneProvinceCount {
    static class PhoneFlowCountMapper extends Mapper<IntWritable, Text, Text, FlowBean> {
        private Text text=new Text();
        private FlowBean flowBean=new FlowBean();

        @Override
        protected void map(IntWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] strings = value.toString().split("\t");
            String phone = strings[1];
            long upFlow = Long.parseLong(strings[strings.length - 3]);
            long downFlow = Long.parseLong(strings[strings.length - 2]);

            flowBean.set(upFlow,downFlow);
            text.set(phone);
            context.write(text,flowBean);
        }
    }

    static class PhoneFlowCountReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
        private FlowBean flowBean=new FlowBean();

        @Override
        protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
            long sumUpFlow = 0;
            long sumDownFlow = 0;
            //遍历所有bean，将其中的上行流量，下行流量分别累加
            for (FlowBean flowBean :
                    values) {
                sumUpFlow += flowBean.getUpFlow();
                sumDownFlow += flowBean.getDownFlow();
                flowBean.set(sumUpFlow,sumDownFlow);
                context.write(key,flowBean);
            }
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration config=new Configuration();
        /*
            可省 本地模式 时默认配置
        //输入输出数据可以设置本地或hdfs，要看配置
        config.set("mapreduce.framework.name", "local");
        config.set("fs.defaultFS","file:///");*/

        /*
        集群模式
        conf.set("mapreduce.framework.name", "yarn");
		conf.set("yarn.resoucemanager.hostname", "192.168.186.139");
		 config.set("fs.defaultFS","hdfs://192.168.186.139:9000");*/
        Job job=Job.getInstance(config);

        //指定本程序的jar包所在的本地路径
        job.setJarByClass(PhoneProvinceCount.class);

        //指定本业务job要使用的mapper/Reducer业务类
        job.setMapOutputValueClass(PhoneFlowCountMapper.class);
        job.setOutputValueClass(PhoneFlowCountReducer.class);

        //指定我们自定义的数据分区器
        job.setPartitionerClass(ProvincePartitioner.class);
        //同时指定相应“分区”数量的reducetask
        job.setNumReduceTasks(1);

        //指定mapper输出数据的kv类型
        job.setMapOutputValueClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);

        //指定最终输出的数据的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        //指定job的输入原始文件所在目录
        FileInputFormat.setInputPaths(job,new Path("H:\\a"));

        Path path = new Path("H:\\b");
        FileSystem fileSystem = FileSystem.get(config);
        if(fileSystem.exists(path))
            fileSystem.delete(path,true);
        //指定job的输出结果所在目录
        FileOutputFormat.setOutputPath(job,path);

        //将job中配置的相关参数，以及job所用的java类所在的jar包，提交给yarn去运行
        /*job.submit();*/
        if(!job.waitForCompletion(true))
            return;
    }
}
